<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/2
 * Time: 10:36
 */

namespace app\api\controller;


use app\common\model\GiftRecord;
use app\common\model\Room;
use app\common\model\RoomBackground;
use app\common\model\RoomBlacklist;
use app\common\model\RoomLog;
use app\common\model\UserGuard;
use app\common\model\Users;
use think\Db;

class Play extends User
{

    /**
     * 房间列表
     */
    public function roomList()
    {
        $map['a.status'] = 1;
        $type = input('post.type');
        if (is_numeric($type) && !empty($type)) $map['a.type'] = $type;
        $model = new Room();
        $rows = $model->getRows($map,$this->user_id);
        api_return(1,'获取成功',$rows);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 聊天室开关公屏
     */
    public function chat()
    {
        $data = input('post.');
        $powerCode = getPowerCode($this->user_id,$data['room_id']);

        if ($powerCode == 0){
            api_return(0,'权限不足');
        }

        if($data['type'] == 'open' && $data['type'] != 'close'){
           api_return(0,'类型错误');
        }
        cache('chat'.$data['room_id'],$data['type']);
        $this->sendRoomMsg($data['room_id'],$data['type']);
        api_return(1,'操作成功');
    }

    /**
     * 进入房间
     */
    public function joinRoom()
    {

        $this->ApiLimit(1,$this->user_id);

        $param = input('post.');
        $model = new Room();

        $data = $model->joinRoom($param['room_id'],$this->user_id,trim($param['password']));

        if ($data !== false) {
            $chat = cache('chat'.$data['room_id']);

            //判断是否开启公屏
            if ($chat == 'open'){
                $data['chat'] = 1;
            }else{
                $data['chat'] = 0;
            }


            //贵族加入房间增加热度房间
            $noble_id = self::checkNoble($this->userExtra('user_id,noble_id,noble_time'));
            if ($noble_id == 5 || $noble_id == 4){
                if ($noble_id == 5){
                    $hot = 1000;
                }else{
                    $hot = 500;
                }
                $in  = cache('hotRoom_'.$param['room_id'].'_'.$this->user_id);
                if (!$in){
                    $num = cache('noble_hot_'.$this->user_id);
                    if ($num < 5){
                        addHot($data['room_id'],$hot,7200);
                        cache('noble_hot_'.$this->user_id,$num+1,todayEndTime());
                        cache('hotRoom_'.$param['room_id'].'_'.$this->user_id,true,todayEndTime());
                    }
                }

            }

            $data['noble_id'] = $noble_id;
            $data['level'] = $this->userExtra('level');
            //查询当前用户是否被禁言



            $data['baned'] = $this->chatbanList($data['room_id'],$this->user_id);
            //修改当前用户所在房间
            Db::name('user_extend')->where('user_id',$this->user_id)->update(['room_id'=>$data['room_id']]);

            //增加房间热度
            addHot($param['room_id']);

            api_return(1,'获取成功',$data);
        }
        api_return(0,$model->getError());
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 退出房间
     */
    public function outRoom()
    {
        $room_id = input('post.room_id');
        $data['room_id'] = 0;
        self::updateUserExtend($this->user_id,$data);
        //用户上麦成功 或在排麦中 清除排麦数据
        $refresh = self::outRoomFunc($this->user_id,$room_id);
        if ($refresh){
            //需要刷新麦序
            $this->refreshWheat($room_id);
        }
        api_return(1,'退出房间成功');
    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间在线用户列表
     */
    public function onlineUser()
    {
        $map = [];
        $id  = input('post.room_id');
        if (!is_numeric($id)) api_return(0,'请输入房间id');
        $map['e.room_id']       = $id;
        $map['e.online_status'] = 1;
        $model = new Users();
        $rows = $model->getItems($map,$id);

        api_return(1,'获取成功',$rows);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间日志
     */
    public function roomLog()
    {
        $map['room_id'] = input('post.room_id');

        if (!is_numeric($map['room_id'])) api_return(0,'房间id错误');

        $model = new RoomLog();

        $rows  = $model->getRows($map);

        api_return(1,'获取成功',$rows);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间黑名单列表
     */
    public function blackList()
    {
        $map['a.room_id'] = input('post.room_id');
        if (!is_numeric($map['a.room_id'])) api_return(0,'房间id错误');

        $map['a.status'] = 1;
        $map['a.end_time'] = ['>',time()];

        $model = new RoomBlacklist();

        $rows  = $model->getRows($map);

        api_return(1,'获取成功',$rows);

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间打赏列表
     */
    public function roomGift()
    {

        $map['a.room_id'] = input('post.room_id');
        if (!is_numeric($map['a.room_id'])){
            api_return(0,'参数错误');
        }
        $map['a.status']  = 1;
        $model = new GiftRecord();

        $rows = $model->giftList($map);
        $rows['total'] = $model->alias('a')->where($map)->sum('total');
        api_return(1,'获取成功',$rows);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 真爱排行榜
     */
    public function trueLove()
    {

        $map['a.status'] = 1;
        $map['a.room_id'] = ['>',0];
        $map['g.price'] = ['>',519];
        $model = new GiftRecord();

        $rows = $model->getTrueLove($map);

        api_return(1,'获取成功',$rows);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 守护排行榜
     */
    public function protect()
    {
        $map['a.status'] = 1;
        $map['a.room_id'] = ['>',0];
        $model = new GiftRecord();
        $rows = $model->protect($map);
        api_return(1,'获取成功',$rows);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取CP排行榜
     */
    public function guard()
    {
        $time = cache('guardTime');
        $map = [];

        if (time()+86430 > $time){
            $time = time() + 86430;
            cache('guardTime',$time);
        }

        $map['a.status'] = 1;
        $map['a.end_time'] = ['>',$time];

        $model = new UserGuard();
        $rows = $model->getRows($map);

        api_return(1,'获取成功',$rows);

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取贵族排行榜
     */
    public function noble()
    {

        $time = cache('nobleTime');
        $map = [];

        if (time()+30 > $time){
            $time = time() + 30;
            cache('nobleTime',$time);
        }

        $map['e.noble_id']   = ['>',0];
        $map['e.noble_time'] = ['>',$time];

        $model = new Users();

        $rows = $model->getNoble($map);

        api_return(1,'获取成功',$rows);

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 创建房间
     */
    public function createRoom()
    {

        $data = request()->only(['room_name','img','cid','is_lock','password','notice','id'],'post');

        if (is_numeric($data['id'])){

        }else{
            $extra = self::userExtra('noble_id,user_id,noble_time',$this->user_id);
            $noble_id = self::checkNoble($extra);

            if ($noble_id == 0) api_return(0,'非贵族不能创建房间');

            $map['user_id'] = $this->user_id;
            $map['status']  = 1;
            $room_id = Db::name('room')->where($map)->value('room_id');

            if ($room_id) api_return(0,'您已创建了房间,不能重复创建');

            $data['type'] = 4;
            $data['user_id'] = $this->user_id;
            $data['bg_id'] = Db::name('room_background')->where('status',1)->order('sort')->value('bg_id');
        }

        $model = new \app\common\logic\Room();

        $result = $model->saveChange($data);

        if ($result !== false){
            api_return(1,'创建成功',['room_id'=> $model->getLastInsID()]);
        }
        api_return(0,$model->getError());

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 自己创建房间分类获取
     */
    public function roomCate()
    {
        $rows = Db::name('room_category')->where('status',1)->field('cid,cate_name')->select();
        api_return(1,'获取成功',$rows);
    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 已创建房间和家族id返回接口
     */
    public function myCircle()
    {
        $map['user_id'] = $this->user_id;
        $map['status']  = 1;

        $rows['room_id'] = Db::name('room')->where($map)->value('room_id')??0;

        $where['group_user'] = $this->user_id;
        $where['type']   = 2;
        $where['status'] = 1;

        $noble = $this->userExtra('noble_id,noble_time,user_id');

        $rows['noble_id'] = self::checkNoble($noble);

        $rows['group_id'] = Db::name('chat_group')->where($where)->value('group_id')??0;

        api_return(1,'获取成功',$rows);


    }


    
    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间背景列表
     */
    public function bgList()
    {
        $rows = RoomBackground::field('bg_id,img,brief')->order('sort')->cache(3)->select();
        api_return(1,'获取成功',$rows);
    }





}